package cn.wolfcode.crm.web.Controller;

import cn.wolfcode.crm.Service.IDepartmentService;
import cn.wolfcode.crm.Service.IEmployeeService;
import cn.wolfcode.crm.Service.IRoleService;
import cn.wolfcode.crm.annotation.RequiredPermission;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDepartmentService departmentService;

    @RequiresPermissions(value = {"员工编辑","employee:list"},logical = Logical.OR)
    @RequestMapping("/list")
    @RequiredPermission(name="员工列表")
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo) {
        model.addAttribute("depts",departmentService.selectAll());
        PageInfo<Employee> pageResult = employeeService.queryForList(qo);
        model.addAttribute("pageResult", pageResult);
        model.addAttribute("qo",qo);
        return "employee/list";//跳转到list.jsp页面/WEB-INF/view/employee/list.flt
    }

    @RequiresPermissions(value = {"员工修改或保存","employee:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="员工保存更新")
    public String saveOrUpdate(Employee employee,Long[] ids) {
        if (employee.getId() == null) {
            //保存操作
            employeeService.insert(employee,ids);
            //todo delete
            System.out.println("员工保存操作");
        }else{
            //更新操作
            employeeService.updateByPrimaryKey(employee);
            //更新关系:先删除关系数据,再保存
            roleService.deletePersonRelation(employee.getId());
            //todo delete
            System.out.println("员工更新操作");
        }
        //保存关系
        if (ids!=null && ids.length>0){
            for (Long role_id : ids) {
                roleService.insertPersonRelation(employee.getId(),role_id);
            }
        }
        return "redirect:/employee/list";//跳转到list.jsp页面/WEB-INF/view/employee/list.flt
    }

    @RequiresPermissions(value = {"员工删除","employee:delete"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if (id != null){
                roleService.deleteByPrimaryKey(id);
                employeeService.deleteByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        JsonResult result = new JsonResult();
        try {
            if (ids != null){
                /*roleService.deleteByPrimaryKey(id);*/
                employeeService.batchDelete(ids);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("批量删除失败");
        }
        return result;
    }



    @RequiresPermissions(value = {"员工编辑","employee:input"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Model model,Long id) {
        //将部门和角色信息显示共享到jsp页面
        model.addAttribute("roles", roleService.selectAll());
        model.addAttribute("depts" ,departmentService.selectAll());
        if (id!=null){
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee",employee);
        }
        return "employee/input";//跳转到list.jsp页面/WEB-INF/view/employee/list.flt
    }



    @RequiresPermissions(value = {"员工导出","employee:exportXls"},logical = Logical.OR)
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException{
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook book = employeeService.exportXls();
        book.write(response.getOutputStream());
    }


    @RequiresPermissions(value = {"员工导入","employee:importXls"},logical = Logical.OR)
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file){
        JsonResult result = new JsonResult();
        try {
            employeeService.importXls(file);

        }catch (Exception e){
            e.printStackTrace();
            result.mark("导入失败");
        }
        return result;
    }
}
